package com.esoon.service.impl;

import com.esoon.dao.CosLinkManDao;
import com.esoon.dao.ThirdpartyContactsRegionDao;
import com.esoon.entity.CosLinkMan;
import com.esoon.entity.LinkMan;
import com.esoon.result.ResultBody;
import com.esoon.service.LinkManService;
import io.swagger.models.auth.In;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName LinkManServiceImpl
 * @Description TODO
 * @Author 2020/10/19 16:47
 * @Version 1.0
 **/
@Service
public class LinkManServiceImpl implements LinkManService {

    @Resource
    private CosLinkManDao cosLinkManDao;

    @Resource
    private ThirdpartyContactsRegionDao thirdpartyContactsRegionDao;

    private static final Logger logger;

    static {
        logger = LoggerFactory.getLogger(LinkManServiceImpl.class);
    }

    @Override
    public ResultBody getAllLinkMan() {
        try {
            //获取所有的CosLinkMan.
            List<CosLinkMan> cosLinkManList = cosLinkManDao.selectAllLinkMan();
            List<LinkMan> linkManList = new ArrayList<>();
            for (CosLinkMan cosLinkMan : cosLinkManList) {
                String lmName = cosLinkMan.getLmName();
                String lmPhone = cosLinkMan.getLmPhone();
                //获取绑定的部门信息
                String field = cosLinkMan.getField1();
                //根据部门信息查询当前的部门名 crName
                String twoName = thirdpartyContactsRegionDao.selectCrNameByCrId(Integer.valueOf(field));
                String oneName = thirdpartyContactsRegionDao.selectCrNameByCrParent(Integer.valueOf(field));
                //拼接获取区域名称
                String areaDept = oneName + "-" + twoName;
                //生成linkMan对象
                LinkMan linkMan = new LinkMan();
                linkMan.setAreaDept(areaDept);
                linkMan.setLmName(lmName);
                linkMan.setLmPhone(lmPhone);
                linkManList.add(linkMan);
            }
            logger.info("获取联系人信息成功");
            return new ResultBody(linkManList);
        }catch (Exception e){
            logger.error("查询异常");
            ResultBody resultBody = new ResultBody();
            resultBody.setCode(500);
            resultBody.setMsg("查询异常");
            return resultBody;
        }
    }
}
